<template>
	<!-- image上传 -->
	<view class="wrap">
		<u-upload ref="uUpload1" max-count="6" :file-list="dataimg" :multiple="false" :action="action" delBgColor="#7d7f88"
			@select-file="selectFile" @on-choose-complete="bendiurl" @on-anew="anewimage"
			@on-remove="deleteImg"></u-upload>
	</view>

</template>

<script setup lang="ts">
	import { deleteUnQualifiedImage } from '@/common/api/operationApi'
	import { ref, toRefs } from 'vue'
	import { useUpload } from './uploadImg';
	import { toRaw } from '@vue/reactivity'
	import { BASE_URL } from '@/config';
	const emit = defineEmits(["imageNum"])

	const props = defineProps<{
		id ?: string[] | string;
		dataimg ?: []
	}>()
	const id = toRefs(props).id
	const dataimg = toRefs(props).dataimg

	const { uploadFile } = useUpload();
	const action = ref<string>('');
	const number = ref<number>(0);
	function selectFile() {
		if (oldList.value.length === 0) {
			let num = dataimg.value.length
			number.value = num
			emit('imageNum',number.value)
			let obj = dataimg.value
			oldList.value.push(...obj)
		}
	}

	const uUpload1 = ref(null);
	const oldList = ref<string[]>([]);
	const oldListversions = ref(null);
	function bendiurl(a, b, c) {
		if (c === undefined) {
			oldListversions.value = toRaw(a)[toRaw(a).length - 1]
		} else {
			oldListversions.value = toRaw(a)[c]
		}
		const name = ref<string>('')
		if (oldListversions.value.file.name === undefined) {
			let path = oldListversions.value.url;
			name.value = path.replace(/^.*[\\\/]/, '');
		}
		let abb = id.value.split(",")

		// 上传
		uploadFile({
			url: `${BASE_URL}/pad/padTask/upLoadUnQualifiedFile`,
			filePath: oldListversions.value.url,
			name: 'file',
			data: {
				"taskIds": abb,
				"name": oldListversions.value.file.name !== undefined ? oldListversions.value.file.name : name.value
			},
			success(res) {
				if (JSON.parse(res.data).code === 2000) {
					oldListversions.value.id = JSON.parse(res.data).data.id
					if (c === undefined) {
						number.value++
					}
					uUpload1.value.closePromptBox()
				} else {
					if (c === undefined) {
						number.value++
						uUpload1.value.uploadError(number.value - 1)
					} else {
						uUpload1.value.uploadError(c)
					}
					uUpload1.value.closePromptBox()
				}
				emit('imageNum',number.value)
			},
		});
		const uploadError = () => {
			uUpload1.value.uploadFile();
		}
		let obj = oldListversions.value
		oldList.value.push(obj)
	}
	const newList = ref(null);
	const anewimage = (a, b, c) => {
		bendiurl(a, b, c)
		return
	}
	const deleteImg = (a, b, c) => {
		selectFile()
		deletePicture(oldList.value[a].id)
		oldList.value.splice(a, 1)
		number.value--
		emit('imageNum',number.value)
	}
	// 删除
	async function deletePicture(e) {
		let idsp = [e]
		const data = await deleteUnQualifiedImage({
			ids: idsp
		})
	}

	function turnUpTheCamera() {
		uUpload1.value.selectFile();
	}

	defineExpose({
		turnUpTheCamera
	})
</script>

<style scoped>

</style>